package com.zzsin.gtestreporter.controller;

import com.zzsin.gtestreporter.Constants;
import com.zzsin.gtestreporter.report.GTestReport;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

@Controller
public class PageController {
    @RequestMapping("/")
    public String d(HttpServletResponse response, HttpServletRequest request, Model model) {
        GTestReport report = new GTestReport(Constants.LOG_DIR);
        report.parse();
        model.addAttribute("task_list", report.getTtList());
        return "task_list";
    }
    @RequestMapping("/index.html")
    public String indexindex(HttpServletResponse response, HttpServletRequest request, Model model) {
        GTestReport report = new GTestReport(Constants.LOG_DIR);
        report.parse();
        model.addAttribute("task_list", report.getTtList());
        return "task_list";
    }

    @RequestMapping("/gtest_reporter/task_list.html")
    public String task_list(HttpServletResponse response, HttpServletRequest request, Model model) {
        GTestReport report = new GTestReport(Constants.LOG_DIR);
        report.parse();
        model.addAttribute("task_list", report.getTtList());
        return "task_list";
    }

    class Report
    {
        public String title = "";
        public String href = "";
    }
    @RequestMapping("/gtest_reporter/test_task/{task_id}.html")
    public String test_task(HttpServletResponse response, HttpServletRequest request, @PathVariable("task_id")String task_id, Model model) {

        GTestReport report = new GTestReport(Constants.LOG_DIR);
        report.parse();


        List<Report> reportList = new ArrayList<Report>();
        // Constants.LOG_COV;
        {
            File covDir = new File(Constants.LOG_COV + File.separator + task_id);
            for(File f : covDir.listFiles()){
                String fileName = f.getName();


                String aName = fileName;
                String aPath = "/coverage_html_report/" + task_id + "/" + fileName + "/index.html";

                System.out.println(aName);
                System.out.println(aPath);

                Report repo = new Report();
                repo.title = aName;
                repo.href = aPath;
                reportList.add(repo);
            }
        }

        // String cov_report_url = "/coverage_html_report/" + task_id + "/";

        model.addAttribute("cov_report_list",    reportList);

        model.addAttribute("task_id",           task_id);
        model.addAttribute("xml_report_list",   report.getTestTask(task_id).getXmlReportList());
        model.addAttribute("task_count",        report.getTestTask(task_id).getXmlReportList().size());

        return "test_task";
    }
}
